package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallCaller;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  去重来电信息
 * @author liy
 * @datetime 2014年10月13日,下午7:59:07
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallDuplicateRemovalAction extends PublicAction implements ModelDriven<MedicalCallCaller>{
	
	private static final long serialVersionUID = 5709918634714517299L;
	
	private MedicalCallCaller callCaller = new MedicalCallCaller();
	
	/**
	 * @desc 跳转到去重来电信息页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年10月13日,下午7:33:25
	 */
	@Action(value = "call_duplicate_removal_init", results = {@Result(location = "call_duplicate_removal_init.jsp")})
	public String removalInit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转到去重来电统计页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年10月13日,下午7:33:25
	 */
	@Action(value = "call_duplicate_removal_statistics_init", results = {@Result(location = "call_duplicate_removal_statistics_init.jsp")})
	public String statisticsInit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 去重来电信息查询
	 * @return null
	 * @author liy
	 * @datetime 2014年12月16日,上午12:06:21
	 */
	@Action(value = "call_duplicate_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		pageBean = this.baseAllService.getTelService().fingCallCallerByPage(callCaller, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 去重来电信息统计
	 * @return null
	 * @author liy
	 * @datetime 2014年12月16日,下午12:07:51
	 */
	@Action(value = "call_duplicate_statistics")
	public String statistics() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		pageBean = this.baseAllService.getTelService().fingDuplicateCallCallerByPage(callCaller, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 去重来电信息导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月16日,上午11:03:04
	 */
	@Action(value = "call_duplicate_view_exports")
	public String exportsView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			callCaller.setHospitalId(user.getEnterpriseId());
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"去重来电信息导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getTelService().currentDuplicateExport(user,callCaller);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 去重来电统计导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月16日,下午6:35:20
	 */
	@Action(value = "call_duplicate_sta_exports")
	public String exportsSta() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			callCaller.setHospitalId(user.getEnterpriseId());
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"去重来电信息导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getTelService().duplicateStaExport(user,callCaller);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalCallCaller getModel() {
		return callCaller;
	}

}
